<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.title" size="medium" placeholder="" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />

      <el-button v-waves class="filter-item" type="primary" size="medium" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" size="medium" icon="el-icon-edit" @click="handleCreate">
        添加
      </el-button>

      <div class="block">
        <el-table
          :data="tableData"
          style="width: 100%;margin-bottom: 20px;"
          row-key="id"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
          <el-table-column
            prop="display_name"
            label="权限名称"
            width="180">
          </el-table-column>
          <el-table-column
            prop="effect_uri"
            label="路由"
            width="180">
          </el-table-column>
          <el-table-column
            prop="description"
            label="描述">
          </el-table-column>
          <el-table-column
            prop="order"
            label="排序"
            width="100">
          </el-table-column>
          <el-table-column
            prop="create_time"
            label="创建日期"
            width="180">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" @click="handleAdd(scope.$index, scope.row)">添加</el-button>
              <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" @close="closeDialog">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="80px" style="width: 400px; margin-left:50px;">
        <el-form-item label="权限名" prop="display_name">
          <el-input v-model="temp.display_name" placeholder="输入权限名称" />
        </el-form-item>
        <el-form-item label="权限标识" prop="name">
          <el-input v-model="temp.name" placeholder="输入权限标识" />
        </el-form-item>
        <el-form-item label="父级" prop="parent_id">
          <el-cascader
            v-model="cascader_val"
            :options="tableData"
            :props="props"
            placeholder="选择父级"
            @change="changeParent"
            clearable ></el-cascader>
        </el-form-item>
        <el-form-item label="路由" prop="effect_uri">
          <el-input v-model="temp.effect_uri" placeholder="输入路由">
          </el-input>
        </el-form-item>
        <el-form-item label="排序" prop="order">
          <el-input v-model="temp.order" placeholder="输入排序" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="temp.description" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="菜单描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import waves from '@/directive/waves'
import { createPermission, delPermission, getPermissionList, updatePermission } from '@/api/permission'
export default {
  name: 'Permission',
  directives: { waves },
  data() {
    return {
      listQuery: {
        title: ''
      },
      tableData: [],
      textMap: {
        update: '编辑',
        create: '添加'
      },
      dialogStatus: '',
      dialogFormVisible: false,
      rules: {
        display_name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        effect_uri: [{ required: true, message: '请选输入路由', trigger: 'blur' }]
      },
      temp: {
        id: undefined,
        parent_id: 0,
        name: '',
        display_name: '',
        effect_uri: '',
        description: '',
        order: 99
      },
      props: {
        value: 'id',
        label: 'display_name',
        children: 'children',
        checkStrictly: true
      },
      cascader_val: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      getPermissionList(this.listQuery).then(response => {
        if (response.code === 200) {
          this.tableData = response.data.row
        }
        this.resetTemp()
      })
    },
    handleFilter() {
      var that = this
      that.getList()
    },
    resetTemp() {
      this.cascader_val = []
      this.temp = {
        id: undefined,
        parent_id: 0,
        name: '',
        display_name: '',
        effect_uri: '',
        description: '',
        order: 99
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleEdit(index, row) {
      this.cascader_val = [row.parent_id]
      this.temp = {
        id: row.id,
        parent_id: row.parent_id,
        name: row.name,
        display_name: row.display_name,
        effect_uri: row.effect_uri,
        description: row.description,
        order: 99
      }
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDelete(index, row) {
      delPermission(row.id).then(response => {
        if (response.code === 200) {
          this.$message({
            message: response.msg,
            type: 'success'
          })
          this.dialogFormVisible = false
          this.getList()
        }
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          createPermission(this.temp).then(response => {
            if (response.code === 200) {
              this.$message({
                message: response.msg,
                type: 'success'
              })
              this.dialogFormVisible = false
              this.getList()
            }
          })
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          console.log(this.temp)
          if (this.temp.parent_id === undefined) {
            this.temp.parent_id = 0
          }
          updatePermission(this.temp).then(response => {
            if (response.code === 200) {
              this.$message({
                message: response.msg,
                type: 'success'
              })
              this.dialogFormVisible = false
              this.getList()
            }
          })
        }
      })
    },
    handleAdd(index, row) {
      if (row.depth) {
        switch (row.depth) {
          case 0:
            this.cascader_val = [row.id]
            break
          case 1:
            this.cascader_val = [row.parent_id, row.id]
            break
          default:
            this.$message({
              message: '最多只能三层',
              type: 'warning'
            })
            return
        }
      } else {
        this.cascader_val = [row.id]
      }
      this.temp.parent_id = row.id
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    changeParent(value) {
      this.temp.parent_id = value[value.length - 1]
    },
    closeDialog() {
      this.resetTemp()
    }
  }
}
</script>

<style scoped>

</style>
